package ChatNI;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;


import ourMessage.*;

/**
 *
 * @author Ridiss
 */
public class Sender {
        

         private DatagramSocket socket;
           
    /**
     *
     */
    public Sender() {
                
                try {
                    socket=new DatagramSocket();
                }
                catch(SocketException e){
                }
            }
        
    /**
     *
     * @param m
     * @param UsernameIp
     */
    public void SendMessage(Message m,String UsernameIp){
                try {
            
            byte[]data=Message.toArray(m);
            DatagramPacket packet=new DatagramPacket(data,
            data.length, InetAddress.getByName(UsernameIp), 2014);
            socket.send(packet);
           
        }
        catch(IOException ioe) {
            System.err.println(ioe);
        } 
        }
           
    /**
     *
     * @param m
     *          m pour message
     */
    public void BroadCastMessage(Message m){
                try {
         
            byte[]data=Message.toArray(m);
            
            DatagramPacket packet=new DatagramPacket(data,
            data.length,InetAddress.getByName("255.255.255.255"), 2014);
           
            socket.send(packet);
           
        }
        catch(IOException ioe) {
            System.err.println(ioe);
        } 
        }
        
    /**
     *
     * @param m
     * @param UsernameIpList
     */
    public void MultiCastMessage(Message m,String[] UsernameIpList){
                
        }

    /**
     *
     */
    public void closeSendSocket() {
                // TODO Auto-generated method stub
                  try {
                    socket.close();
                }
                catch(Exception e) {
                }
        }

        
}
